require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#myPost",
    data:{
        loadFlag:false,
        value:1,
        // loginUid:userInfo.id,
        pageNumber:1,
        isScreen:"",
        myPostsList:{},//我的帖子
        myPostsList1:[],
        myPostsList2:[],
        friendDynamic:{},//好友动态列表
        friendDynamic1:[],
        friendDynamic2:[],
        pop:{
            question: "您确定要删除此帖？",
            show: false
        },
        postId:"",
        commentList:{},
        commentList1:[],
        commentList2:{},
        // postId:"",//帖子id
        commentText:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.circleId=this.searchObject&&this.searchObject.circleId;
        this.loginUid=this.searchObject&&this.searchObject.uid;
        // this.isMain=this.searchObject&&this.searchObject.isMain;
        //this.getGroupMemberInfo();
        this.myPosts();
        this.myRepliesPosts();
    },
    methods:{
        //后退
        getBack: function () {
            //history.go(-1);
            // if(this.square==1){
            //     location.href="/friend/circleSquare";
            // }else{
            //     location.href="/friend?indexs=3";
            // }
            location.href="/friend?indexs=3";
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //tab切换
        getTab:function(num){
            this.value=num;
            this.pageNumber=1;
        },
        //我的帖子
        myPosts:function(){
            var that=this;
            var data={
                // loginUid:that.loginUid,
                loginUid:userInfo.id,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/friend/myPosts",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.myPostsList=b.json.data;
                    that.myPostsList1=that.myPostsList.list;
                    if(that.myPostsList.pageNumber>1){
                        if(that.myPostsList.list.length>0) {
                            that.myPostsList2 = that.myPostsList2.concat(b.json.data.list);
                        }
                    }else{
                        that.myPostsList2= that.myPostsList1;
                    }
                    console.log(that.myPostsList2)
                    vm.loadFlag=false;
                }else{
                    that.myPostsList={};
                    that.myPostsList1=[];
                    that.myPostsList2=[];
                    vm.$refs.prompt.toast("获取我的帖子失败！")
                }
            });
        },
        //我的回帖
        myRepliesPosts:function(){
            var that=this;
            var data={
                // loginUid:that.loginUid,
                loginUid:userInfo.id,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/friend/myRepliesPosts",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.friendDynamic=b.json.data;
                    that.friendDynamic1=that.friendDynamic.list;
                    if(that.friendDynamic.pageNumber>1){
                        if(that.friendDynamic.list.length>0) {
                            that.friendDynamic2 = that.friendDynamic2.concat(b.json.data.list);
                        }
                    }else{
                        that.friendDynamic2= that.friendDynamic1;
                    }
                    vm.loadFlag=false;
                    // lazyload();
                }else{
                    that.friendDynamic={};
                    that.friendDynamic1=[];
                    that.friendDynamic2=[];
                    vm.$refs.prompt.toast("获取回帖失败！")
                }
            });
        },
        getIsClassify:function(num){
            if(num){
                this.getIsScreen();
            }
            this.pop.show=!this.pop.show;
        },
        getIsScreen:function(num,postId){
            this.isScreen=num;
            if(num&&postId){
                this.postId=postId;
            }
        },
        //删除帖子
        deletePost:function(){
            var that=this;
            var data={
                // loginUid:that.loginUid,
                loginUid:userInfo.id,
                postId:that.postId,
                delType:3
            };
            console.log(data);
            that.getIsClassify();
            this.ajax({url:"/friend/deletePost",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.myPosts();
                }else{
                    vm.$refs.prompt.toast("删除帖子失败！");
                }
            });
        },
        //获取自媒体评论列表
        getInfoMediaAdvCommentList:function(){
            var that=this;
            var data={
                loginUid:userInfo.id,
                postId:that.postId,
                pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/friend/getInfoPostCommentList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentList=b.json.data;
                    that.commentList1=that.commentList.list;
                    if(that.commentList.pageNumber>1){
                        if(that.commentList.list.length>0) {
                            that.commentList2 = that.commentList2.concat(b.json.data.list);
                        }
                    }else{
                        that.commentList2= that.commentList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentList)
                }else{
                    that.commentList={};
                    that.commentList1=[];
                    that.commentList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        getCommentInfo:function(item){
            console.log(item);
            location.href="/friend/postDetails?postId="+item.postId+"&uid="+userInfo.id;
        },
        getNext:function(){
            location.href="/friend/postRecycling"
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag;
    if(vm.isScreen==2){
        loadFlag = vm.commentList.lastPage;
    }else{
        if(vm.value==1){
            loadFlag = vm.myPostsList.lastPage;
        }else if(vm.value==2){
            loadFlag = vm.friendDynamic.lastPage;
        }
    }
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            if(vm.isScreen==2){
                vm.getInfoMediaAdvCommentList();
            }else{
                if(vm.value==1){
                    vm.myPosts();
                }else if(vm.value==2){
                    vm.myRepliesPosts();
                }
            }
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};